package com.travel.manager.quartz;


import com.travel.common.pojo.IndexAdv;
import com.travel.common.util.Constants;
import com.travel.manager.service.IndexAdvService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class AdvJob{
    @Autowired
    private IndexAdvService indexAdvService;

    public void execute() {
        //更新状态
        Date today = getStartOfDay(new Date(), Calendar.getInstance());
        IndexAdv indexAdv = new IndexAdv();
        indexAdv.setStatus(Constants.ADV_STATUS_SHOW);//查询正在显示的广告位
        List<IndexAdv> indexAdvList = indexAdvService.getIndexAdvForUpdate(indexAdv);
        for (IndexAdv adv : indexAdvList) {
            if (adv.getEndDate().before(today)) {//如果在今天之前结束
                adv.setStatus(Constants.ADV_STATUS_TIMEOUT);//下架
                indexAdvService.updateIndexAdvStatus(adv);
            }
        }
        indexAdv.setStatus(Constants.ADV_STATUS_CREATED);//查询新创建的广告位
        indexAdvList = indexAdvService.getIndexAdvForUpdate(indexAdv);
        for (IndexAdv adv : indexAdvList) {//上架
            if (adv.getStartDate().before(today)) {
                adv.setStatus(Constants.ADV_STATUS_SHOW);
                indexAdvService.updateIndexAdvStatus(adv);
                //indexAdvService.exportDetailPage(adv.getHotel().getId());
            }
        }
        indexAdvService.exportIndexPage();
    }

    //获取当天的开始时间
    public static Date getStartOfDay(Date day, Calendar cal) {
        if (day == null)
            day = new Date();
        cal.setTime(day);
        cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND)+1);
        cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));
        return cal.getTime();
    }

}
